package com.jimikongjian.service;

import com.jimikongjian.dao.IPhoto;
import com.jimikongjian.models.Message;
import com.jimikongjian.models.Photo;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.IAlbumService;
import com.jimikongjian.service.base.IPhotoService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/13.
 */
@Service("photoService")
public class PhotoService implements IPhotoService{

    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iPhotoImpl")
    private IPhoto iPhotoImpl;


    @Autowired
    @Qualifier("albumService")
    private IAlbumService albumService;


    /**
     * 上传照片列表
     *
     * @param photoList
     * @return
     */
    public ResponseMessage uploadPhotoList(List<Photo> photoList) {
        for (int i = 0; i < photoList.size(); i ++){
            uploadPhoto(photoList.get(i));
            if (photoList.get(i).getPhotoKey() != ""
                    && photoList.get(i).getAlbumId() > 0){
                if (albumService.hasAlbum(photoList.get(i).getAlbumId())){
                    iPhotoImpl.insertPhoto(photoList.get(i));
                }else {
                    return responseMessageService.makeMessage(Message.NO_HAVE_ALBUM);
                }
            }else {
                return responseMessageService.makeMessage(Message.PARAMS_ERROR);
            }
        }
        return responseMessageService.makeMessage(Message.SUCCESS);
    }

    /**
     * 上传照片
     *
     * @param photo
     * @return
     */
    public ResponseMessage uploadPhoto(Photo photo) {
        if (photo.getPhotoKey() != ""
                && photo.getAlbumId() > 0){
            if (albumService.hasAlbum(photo.getAlbumId())){
                iPhotoImpl.insertPhoto(photo);
                return responseMessageService.makeMessage(Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ALBUM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过相片的id号，查找该照片
     *
     * @param id
     * @return
     */
    public ResponseMessage selectPhotoById(int id) {
        if (id > 0){
            Photo photo = iPhotoImpl.getPhotoById(id);
            return responseMessageService.makeMessage(photo, Message.SUCCESS);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 通过相册的id号查找该相册下的从第start个开始到第start+length个照片
     *
     * @param albumId
     * @param start
     * @param length  @return
     */
    public ResponseMessage selectPhotoByAlbumId(int albumId, int start, int length) {
        if (albumId > 0 && start >= 0 && length > 0){
            if (albumService.hasAlbum(albumId)){
                List<Photo> photoList = iPhotoImpl.getPhotoListByAlbumId(albumId, start ,length);
                return responseMessageService.makeMessage(photoList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_ALBUM);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
